package com.ptsolusi.ceriacrudwicket.tool.models;

import org.apache.wicket.model.LoadableDetachableModel;

import com.ptsolusi.ceriacrudwicket.logic.CustomerLogic;
import com.ptsolusi.ceriacrudwicket.model.Customer;
import com.ptsolusi.ceriacrudwicket.tool.Locator;

public class DetachableCustomerModel extends LoadableDetachableModel {
	
	private static final long serialVersionUID = 1L;
	private final long id;
	
	/**
	 * @return
	 */
	protected CustomerLogic getCustomerLogic(){
		return Locator.getCustomerLogic();
	}
	
	/**
	 * @param customer
	 */
	public DetachableCustomerModel(Customer customer){
		this(customer.getId());
	}

	/**
	 * @param id
	 */
	public DetachableCustomerModel(long id) {
		if (id == 0) {
			throw new IllegalArgumentException();
		}
		this.id = id;
	}
	
	public boolean equals(final Object obj) {
		if (obj == this) {
			return true;
		} else if (obj == null) {
			return false;
		} else if (obj instanceof DetachableCustomerModel) {
			DetachableCustomerModel other = (DetachableCustomerModel) obj;
			return other.id == id;
		}
		return false;
	}

	@Override
	protected Object load() {
		return getCustomerLogic().getItemById(id);
	}
	
	/* (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		return Long.valueOf(id).hashCode();
	}

}
